//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Ipc
{
    public class IpcServer<TData> : IDisposable
    {
        private bool disposed = false;
        private string eventName;
        IntPtr queueHandle = PointToPointMessageQueueHelper.INVALID_HANDLE;

        public void Initialize(String eventName)
        {
            this.eventName = eventName;

            queueHandle = PointToPointMessageQueueHelper.CreateMessageQueue(eventName, false);
        }

        public void RaiseEvent(TData data)
        {
            XmlSerializer serializer = new XmlSerializer(data.GetType());
            MemoryStream memoryStream = new MemoryStream();
            serializer.Serialize(memoryStream, data);
            memoryStream.Seek(0, SeekOrigin.Begin);
            byte[] buffer = memoryStream.ToArray();
            PointToPointMessageQueueHelper.WriteMessageQueue(queueHandle, buffer, buffer.Length);
        }

        #region IDisposable Members

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                }

                if (queueHandle != PointToPointMessageQueueHelper.INVALID_HANDLE)
                {
                    PointToPointMessageQueueHelper.CloseMessageQueue(queueHandle);

                    queueHandle = PointToPointMessageQueueHelper.INVALID_HANDLE;
                }
            }

            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion


        ~IpcServer()
        {
            Dispose(false);
        }

        //private bool disposed = false;

        //public void Dispose()
        //{
        //    Dispose(true);
        //    GC.SuppressFinalize(this);
        //}

        //private void Dispose(bool disposing)
        //{
        //    if(!this.disposed)
        //    {
        //        if(disposing)
        //        {
        //            component.Dispose();
        //        }

        //        //Dispose      
        //    }

        //    disposed = true;         
        //}

        //~class()      
        //{
        //    Dispose(false);
        //}
    }


}
